package com.portal.business.functions;

import java.util.ArrayList;
import java.util.List;

import com.portal.business.functions.dao.FunctionsDao;
import com.portal.business.functions.dto.FunctionsDTO;
import com.portal.business.functions.dto.FunctionsResultBean;
import com.portal.business.price.dao.PriceDao;
import com.portal.framework.common.PortalSearchBean;
import com.portal.framework.page.Pager;


	
/**
 * 
 * 项目名称: Portal     
 * 类名称:   FunctionsServiceImpl     
 * 类描述:   功能介绍  
 * 创建人:   骆海东
 * 创建时间: Aug 31, 2011 5:12:57 PM     
 * 最后修改人:   骆海东
 * 最后修改时间: Aug 31, 2011 5:12:57 PM     
 * 最后修改备注:
 */
public class FunctionsServiceImpl implements FunctionsService {

	private FunctionsDao functionsDao;
	
	private PriceDao priceDao;
	
	public Boolean addFunctions(FunctionsDTO functionsDTO) {
		if(functionsDTO.getShowStatus()==1){//首页展示
			List<FunctionsDTO> fs=this.functionsDao.find("from FunctionsDTO where showStatus=?  order by orderNum desc",1);//获取设置了首页展示的数据
			if(fs.size()==0)functionsDTO.setOrderNum(1);//若数据库还没有设置首页展示的，该数据设置顺序为1
			else {//若有,则获取顺序最大的 加1
				FunctionsDTO f=fs.get(0);
				int order=f.getOrderNum()+1;
				functionsDTO.setOrderNum(order);
			}
		}
		if(this.functionsDao.create(functionsDTO)!=null)return true;
		else return false;
	}

	public int delFunctions(Integer functionsId) {
		FunctionsDTO f=this.functionsDao.findById(functionsId);
		f.setPriceSet(null);
		this.functionsDao.update(f);
		this.functionsDao.refreshSession();
		return 1;
	}

	public int delFunctionsBatch(String[] asIdsStr) {
		int len=asIdsStr.length;
		Integer[] ids=new Integer[len];
		for (int i = 0; i <len; i++) {
			ids[i]=Integer.valueOf(asIdsStr[i]);
		}
		return this.functionsDao.deleteBatch(ids);
	}

	public FunctionsDTO findByID(Integer functionsId) {
		FunctionsDTO f= this.functionsDao.findById(functionsId);
		f.setPriceSet(null);//系统后台调用不了门户项目的延迟加载,要将该数据设为空
		return f;
	}

	public FunctionsResultBean findFunctions(PortalSearchBean portalSearchBean) {

		String selecthql="select new FunctionsDTO(funId,name,fdesc,img,funType,createDate,showStatus,showDesc,orderNum)";
		String countHql="select count(funId) ";
		String hql=" from FunctionsDTO ";
		//用于保存查找输入参数集合
		List<Object> osList=new ArrayList<Object>();
		if(portalSearchBean.getName()!=null&&!portalSearchBean.getName().trim().equals("")){
			hql=hql+"where name like ?";
			osList.add("%"+portalSearchBean.getName()+"%");
		}
		hql=hql+" order by funId desc";
		//获取总数
		int count=this.functionsDao.countQuery(countHql+hql,osList.toArray());
		//设置页码对象
		Pager pager=new Pager();
		pager.setPageSize(portalSearchBean.getPageSize());
		pager.setTotal(count);
		pager.setCurrentPage(portalSearchBean.getPage());
		//获取数据
		List<FunctionsDTO> olsit=this.functionsDao.find(pager.getStart(),pager.getPageSize(), FunctionsDTO.class, selecthql+hql, osList.toArray());
		//设置返回对象
		FunctionsResultBean pb=new FunctionsResultBean();
		pb.setCount(count);
		pb.setResultList(olsit);
		return pb;
	}

	public Boolean updateFunctions(FunctionsDTO functionsDTO) {
		FunctionsDTO old=this.functionsDao.findById(functionsDTO.getFunId());
		if(functionsDTO.getImg()!=null&&!functionsDTO.getImg().equals("")){
			old.setImg(functionsDTO.getImg());
		}
		if(functionsDTO.getShowStatus()==1&&old.getOrderNum()==null){//设置首页展示顺序
			List<FunctionsDTO> fs=this.functionsDao.find("from FunctionsDTO where showStatus=?  order by orderNum desc",1);//获取设置了首页展示的数据
			if(fs.size()==0)old.setOrderNum(1);//若数据库还没有设置首页展示的，该数据设置顺序为1
			else {//若有,则获取顺序最大的 加1
				FunctionsDTO f=fs.get(0);
				int order=f.getOrderNum()+1;
				old.setOrderNum(order);
			}
		}else if(functionsDTO.getShowStatus()==0){
			old.setOrderNum(null);
		}
		old.setFdesc(functionsDTO.getFdesc());
		old.setFunType(functionsDTO.getFunType());
		old.setName(functionsDTO.getName());
//		old.setOrderNum(functionsDTO.getOrderNum());
		old.setShowDesc(functionsDTO.getShowDesc());
		old.setShowStatus(functionsDTO.getShowStatus());
		if(this.functionsDao.update(old)!=null)return true;
		else return false;
	}

	public FunctionsDao getFunctionsDao() {
		return functionsDao;
	}

	public void setFunctionsDao(FunctionsDao functionsDao) {
		this.functionsDao = functionsDao;
	}

	public List<FunctionsDTO> findAllFunctions() {
		String hql="select new FunctionsDTO(funId,name,funType) from FunctionsDTO order by funId desc";
		return this.functionsDao.find(hql);
	}
	
	public List<FunctionsDTO> findAllDesc() {
		String hql="select new FunctionsDTO(funId,name,funType,fdesc) from FunctionsDTO order by funId desc";
		return this.functionsDao.find(hql);
	}

	public List<FunctionsDTO> findAll() {
		return this.functionsDao.findAll();
	}

	public List<FunctionsDTO> findIndex() {
		String hql="select new FunctionsDTO(funId,name,fdesc,img,funType,createDate,showStatus,showDesc,orderNum) from FunctionsDTO" +
				" where showStatus=?  order by orderNum";
		return this.functionsDao.find(hql,1);
	}
	
	public boolean updateFunctionsOrder(String[] idsStr) {
		int len=idsStr.length;
        List<FunctionsDTO> fs=new ArrayList<FunctionsDTO>();
		for (int i = 0; i <len; i++) {
			int id=Integer.valueOf(idsStr[i]);
			FunctionsDTO f=this.functionsDao.findById(id);
			f.setOrderNum(i+1);
			fs.add(f);
		}
		if(this.functionsDao.updateBatch(fs).size()==len)return true;
		else return false;
	}
	
	public PriceDao getPriceDao() {
		return priceDao;
	}

	public void setPriceDao(PriceDao priceDao) {
		this.priceDao = priceDao;
	}





}

		